package com.mediator
{
	import com.build.ProtoBuilderContext;
	import com.common.DateTimeUtils;
	import com.common.FileUtil;
	import com.common.Global;
	import com.common.Mediator;
	import com.constant.LangType;
	import com.core.event.BatchLoaderEvent;
	import com.core.massload.BatchLoader;
	import com.core.massload.MassLoader;
	import com.core.massload.XLoaderItem;
	import com.view.MainView;
	import com.vo.ClassVo;
	
	import flash.desktop.ClipboardFormats;
	import flash.events.MouseEvent;
	import flash.events.NativeDragEvent;
	import flash.filesystem.File;
	import flash.net.SharedObject;
	import flash.utils.getTimer;
	
	import mx.controls.Alert;
	import mx.events.DragEvent;
	import mx.managers.DragManager;

	public class MainViewMediator extends Mediator
	{
		private var view:MainView;

		public function MainViewMediator(view:MainView)
		{
			this.view = view;
			super(view);
		}

		public override function initListener():void
		{
			this.view.logArea.addEventListener(DragEvent.DRAG_ENTER, onRsPanelDragEnter);
			this.view.logArea.addEventListener(NativeDragEvent.NATIVE_DRAG_DROP, onRsPanelDragDrop);
			
			Global.Log = this.Log;
			Global.Error = this.Error;
			
			addEventListener(view.selectPathBtn, MouseEvent.CLICK, onSelectPathClick);
			addEventListener(view.selectServerPathBtn, MouseEvent.CLICK, onSelectServerPathClick);
			addEventListener(view.selectClientPathBtn, MouseEvent.CLICK, onSelectClientPathClick);
		}

		private function onSelectPathClick(e:MouseEvent):void
		{
			FileUtil.openFilePath(function(dir:File):void{
				var tempSo:SharedObject = SharedObject.getLocal(Global.CACHE_PROTO_PATH_KEY, "/"); 
				tempSo.data.info = dir.nativePath;
				view.protoDirPath.text = dir.nativePath;
				Global.ProtoProjectPath = dir.nativePath;
			});
		}

		private function onSelectServerPathClick(e:MouseEvent):void
		{
			FileUtil.openFilePath(function(dir:File):void{
				var tempSo:SharedObject = SharedObject.getLocal(Global.CACHE_Server_PATH_KEY, "/"); 
				tempSo.data.info = dir.nativePath;
				view.serverDirPath.text = dir.nativePath;
				Global.ServerProjectPath = dir.nativePath;
			});
		}

		private function onSelectClientPathClick(e:MouseEvent):void
		{
			FileUtil.openFilePath(function(dir:File):void{
				var tempSo:SharedObject = SharedObject.getLocal(Global.CACHE_Client_PATH_KEY, "/");
				tempSo.data.info = dir.nativePath;
				view.clientDirPath.text = dir.nativePath;
				Global.ClientProjectPath = dir.nativePath;
			});
		}

		private function onRsPanelDragEnter(e:DragEvent):void
		{
			DragManager.acceptDragDrop(this.view.logArea);
		}

		private var xmlList:Array = new Array();

		private function onRsPanelDragDrop(e:NativeDragEvent):void
		{
			xmlList.length = 0;

			var fileList:Array = e.clipboard.getData(ClipboardFormats.FILE_LIST_FORMAT) as Array;
			var xmlPathList:Array = new Array();
			for each(var file:File in fileList){
				if(!file.isDirectory){
					switch(file.type){
						case ".xml":
							if(file.name == "Mid.xml")
							{
								OnXMLListLoadComplete(null);
							}
							else if(file.name == "fieldtype.xml")
							{
								OnXMLListLoadComplete(null);
							}
							else if(file.name == "BehaviorAction.xml")//行为树配置
							{
								OnLoadBehaviorTree(file.nativePath);
							}
							else
							{
								xmlPathList.push(file.nativePath);
							}
							break;
					}
				}
			}

			if(xmlPathList.length > 0)
			{
				if(!Global.ProtoProjectPath)
				{
					Alert.show("请先选择Proto项目路径！");
					return;
				}

				var batch:BatchLoader = new BatchLoader();
				batch.addEventListener(BatchLoaderEvent.BATCH_ITEM_COMPLETE, 
					function(e:BatchLoaderEvent):void
					{
						xmlList.push(new XML(e.item.data));
					}
				);
				batch.addEventListener(BatchLoaderEvent.BATCH_COMPLETE, OnXMLListLoadComplete);

				for each (var str:String in xmlPathList) 
				{
					batch.add(str);
				}
				batch.star();
			}
		}

		/**
		 * 配置都加载完了
		 */		
		private function OnXMLListLoadComplete(e:BatchLoaderEvent):void
		{
			MassLoader.add(new XLoaderItem(Global.ProtoProjectPath+"/Mid.xml", function(item:XLoaderItem):void{
				var midXML:XML = new XML(item.data);
				var now:int = 0;
				for(var i:int = 0; i < xmlList.length; i++)
				{
					now = getTimer();
					var clsList:Vector.<ClassVo> = Global.GetProtoList(xmlList[i]);
					if(clsList.length == 0)
					{
						continue;
					}
					for each (var clsVo:ClassVo in clsList) 
					{
						ProtoBuilderContext.getInstance().build(LangType.CSHARP, clsVo);
					}
					
					Log("C# 生成"+xmlList[i].@name+" 模块完毕！耗时："+(getTimer()-now)+"毫秒");
					
					now = getTimer();
					
					ProtoBuilderContext.getInstance().build(LangType.LUA, clsList);
					
					Log("Lua 生成"+xmlList[i].@name+" 模块完毕！耗时："+(getTimer()-now)+"毫秒");

				}
				now = getTimer();
				Global.BuildMSG(midXML);
				Log("生成MSG完毕！耗时："+(getTimer()-now)+"毫秒");

				MassLoader.add(new XLoaderItem(Global.ProtoProjectPath+"/fieldtype.xml", function(item:XLoaderItem):void{
					var fieldType:XML = new XML(item.data);
					now = getTimer();
					Global.BuildConst(fieldType);
					Log("生成fieldtype完毕！耗时："+(getTimer()-now)+"毫秒");
				}));
			}));
		}

		private function OnLoadBehaviorTree(file:String):void
		{
			MassLoader.add(new XLoaderItem(file, function(item:XLoaderItem):void{
				var btXML:XML = new XML(item.data);
				var now:int = getTimer(); 
				for each(var group:XML in btXML.group)
				{
					var clsList:Vector.<ClassVo> = Global.GetProtoList(group);
					for each (var clsVo:ClassVo in clsList) 
					{
						ProtoBuilderContext.getInstance().build(LangType.CSHARP_BT, clsVo);
					}
				}
				Log("行为树Proto输出完毕！耗时："+(getTimer()-now)+"毫秒");
			}));
		}
		
		private function Log(str:String):void
		{
			this.view.logArea.appendText(DateTimeUtils.formatFullDateTime(new Date())+"\n");
			this.view.logArea.appendText(str+"\n\n");
		}
		
		private function Error(str:String):void
		{
			this.view.logArea.appendText(DateTimeUtils.formatFullDateTime(new Date())+"\n");
			this.view.logArea.appendText("ERROR-------------------------------------\n"+str+"\n\n");
		}
	}
}